package com.yh.csx.crm.core.util;

import com.alibaba.dubbo.remoting.transport.ExceedPayloadLimitException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: chejiangyi
 * @create: 2019-02-28 14:38
 **/
@Slf4j
@Component
public class SapUtils {
    @Value("${sap.customer.url}")
    public void setSap_customer_url(String sap_customer_url) {
        SapUtils.sap_customer_url = sap_customer_url;
    }
    public static String sap_customer_url;

    @Value("${sap.customer.update.url}")
    public void setSap_update_customer_url(String sap_update_customer_url) {
        SapUtils.sap_update_customer_url = sap_update_customer_url;
    }

    public static String sap_update_customer_url;

    @Value("${sap.customer.user}")
    public void setSap_customer_user(String sap_customer_user) {
        SapUtils.sap_customer_user = sap_customer_user;
    }
    public static String sap_customer_user;

    @Value("${sap.customer.password}")
    public void setSap_customer_password(String sap_customer_password) {
        SapUtils.sap_customer_password = sap_customer_password;
    }
    public static String sap_customer_password;

    public static String sendHttpPostAuth(String url, String request_str, String userName, String password, Header header) {
        CloseableHttpResponse response = null;
        if (!StringUtils.isBlank(userName) && !StringUtils.isBlank(password)) {
            HttpClientBuilder cb = HttpClientBuilder.create();
            if (StringUtils.isNotBlank(userName) && StringUtils.isNoneBlank(new CharSequence[]{password})) {
                // 设置BasicAuth
                CredentialsProvider cp = new BasicCredentialsProvider();
                AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
                // 在此处填写用户名和密码
                UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password);
                // Inject the credentials
                cp.setCredentials(scope, credentials);
                // Set the default credentials provider
                cb.setDefaultCredentialsProvider(cp);
            }

            CloseableHttpClient httpClientPwd = cb.build();

//            String requestContent;
            String responseContent;
            try {
                HttpPost httpPost = new HttpPost(url);
                header = null == header ? new BasicHeader("Content-Type", "application/json") : header;
                httpPost.addHeader((Header)header);
                httpPost.setEntity(new StringEntity(request_str,"UTF-8"));
                log.info("请求post:{}",httpPost);
                log.info("url:{},name:{},pwd:{},param:{},hear:{}",url,userName,password,request_str,header);
                response = httpClientPwd.execute(httpPost);
                HttpEntity entity = response.getEntity();
                responseContent = EntityUtils.toString(entity, "utf-8");
            } catch (Exception var14) {
                log.error("HttpClientUtil sendHttpPostAuth failed", var14);
                throw new RuntimeException(var14);
            } finally {
                if(response!=null)
                   try{ response.close();}catch (Exception e){}
            }

            return responseContent;
        } else {
            throw new RuntimeException("sendHttpPostAuth username or password is empty");
        }
    }
}
